package com.postsuperman.proxypool.parser;

import com.postsuperman.proxypool.common.enums.LevelEnum;
import com.postsuperman.proxypool.parser.IParser;
import com.postsuperman.proxypool.pojo.HttpProxy;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ： Lwl postserupman
 * @Description TODO
 * @Date 2019/2/27 12:06
 * @Version 1.0
 */
public class Ip66ProxyParser implements IParser {

    @Override
    public List<HttpProxy> parser(String result) {
        List<HttpProxy> httpProxies = new ArrayList<>();
        if (result != null) {
            Document document = Jsoup.parse(result);
            Elements elements = document.select(".containerbox table tr:gt(0)");

            if (elements != null && elements.size() > 0) {

                HttpProxy httpProxy = null;
                for (Element element : elements) {
                    httpProxy = new HttpProxy();
                    httpProxy.setIp(element.select("td:nth-child(1)").text());
                    httpProxy.setPort(Integer.parseInt(element.select("td:nth-child(2)").text()));
                    httpProxy.setCity(element.select("td:nth-child(3)").text());
                    httpProxy.setLevel(LevelEnum.FREE.getValue());
                    httpProxies.add(httpProxy);
                }
            }
        }

        return httpProxies;
    }
}
